package org.example.ui.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.example.application.UrlShortenerService;
import org.example.domain.model.entity.url.UrlShortener;
import org.example.domain.model.entity.url.req.UrlShortenerCreateReq;
import org.example.domain.model.entity.url.req.UrlShortenerSelectReq;
import org.example.domain.model.entity.url.req.UrlShortenerUpdateReq;
import org.example.domain.model.entity.url.resp.QueryGroupByCountResp;
import org.example.web.result.ResultInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v1")
@Tag(name = "短链接模块" ,description = "短链接功能")
@RequiredArgsConstructor
public class UrlShortenerController {

    private final UrlShortenerService urlShortenerService;

    @PostMapping("/create")
    @Operation(summary = "创建短链接")
    private ResultInfo createUrlShortener(@RequestBody UrlShortenerCreateReq urlshortenerCreateReq){
        return ResultInfo.success(urlShortenerService.createUrlShortener(urlshortenerCreateReq));
    }



    @PostMapping("/update")
    @Operation(summary = "修改短链接")
    public ResultInfo<Void> updateUrlShortener(@RequestBody UrlShortenerUpdateReq urlShortenerUpdateReq) {
        urlShortenerService.updateUrlShortener(urlShortenerUpdateReq);
        return ResultInfo.success();
    }

    @PostMapping("/selectByPage")
    @Operation(summary = "分页查询短链接")
    public ResultInfo<?> selectByPage(@RequestBody UrlShortenerSelectReq urlShortenerSelectReq) {
        PageInfo<UrlShortener> selectByPage =  urlShortenerService.selectByPage(urlShortenerSelectReq);
        return ResultInfo.success(selectByPage);
    }


    @PostMapping("/queryGroupByCount")
    @Operation(summary = "查询短链接分组内数量")
    public ResultInfo<?> queryGroupByCount(@RequestBody List<String> gidList) {
        List<QueryGroupByCountResp> queryGroupByCountRespList = urlShortenerService.queryGroupByCount(gidList);
        return ResultInfo.success(queryGroupByCountRespList);
    }

}
